'''
Created on 2011-5-17

@author: sevear
'''
import chaosdb,p2p

def synchronousSiteListenr(user):
#    user = chaosdb.getDBUser(listenerID)
    if(user!=None and user.tolist!=None):
        contacts = user.tolist.split(",")
        for contactStr in contacts:
            if(contactStr!=''):
                site = chaosdb.getP2pSite(int(contactStr))
                if(site!=None):
                    p2p.addSite(user.uid, site.userid)
                    print "site %d added %s as listener"%(site.userid,user.uid)
                else:
                    print "site %s does not exits"%contactStr
    else:
        print "user %d does not have any contact item"%user.uid
        
def synchronousSiteInfo():
#    rst =  chaosdb.db.select('user',where="isactivate=1")
#    createdSitesCount=0
#    for user in rst:
#        site = chaosdb.getP2pSite(user.uid)
#        if(site==None):
#            p2p.createP2pSite(user.uid)
#            createdSitesCount
#            print "site %d/%s has created"%(user.uid,user.name+"MHz")
#    
#    print "%d sites have been created"
    rst =  chaosdb.db.select('user',where="isactivate=1")
    for user in rst:
        synchronousSiteListenr(user)
          
if __name__ == "__main__": 
    synchronousSiteInfo()
